//
//  DetailScreenTableViewController.m
//  Car
//
//  Created by lanou on 15/9/21.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "DetailScreenTableViewController.h"
#import "LTableViewCell.h"
#import "BrandModel.h"

@interface DetailScreenTableViewController ()
@property (nonatomic ,strong)NSMutableArray *allArray;
@property (nonatomic ,strong)NSMutableArray *array;
@property (nonatomic ,strong)NSMutableArray *dataArray;
@end

@implementation DetailScreenTableViewController
{
    NSMutableArray *contacts;
}
- (NSMutableArray *)array{
    if (!_array) {
        _array = [[NSMutableArray alloc]init];
    }
    return _array;
}
- (NSMutableArray *)allArray{
    if (!_allArray) {
        _allArray = [[NSMutableArray alloc]init];
    }
    return _allArray;
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpData];
    [self reloadAndJosn];
    
    
}
- (void)setUpData{
    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:(UIBarButtonItemStylePlain) target:self action:@selector(buttonAction:)];
    self.navigationItem.leftBarButtonItem = button;
    [self .tableView registerClass:[LTableViewCell class] forCellReuseIdentifier:@"LTCell"];

    NSArray *arr2 = @[@"不限",@"中国",@"德国",@"日本",@"美国",@"韩国",@"法国",@"英国",@"意大利",@"瑞典",@"荷兰",@"捷克",@"西班牙"];
    NSArray *arr3 = @[@"不限",@"2座",@"4座",@"5座",@"6座",@"7座",@"7座以上"];
    NSArray *arr4 = @[@"不限",@"前驱",@"后驱",@"四驱"];
    NSArray *arr5 = @[@"不限",@"汽油",@"柴油",@"电动",@"油电混合"];
    NSArray *arr6 = @[@"1.0",@"1.1-1.6",@"1.7-2.0",@"2.1-2.5",@"2.6-3.0",@"3.1-4.0",@"4.0以上"];
    self.array = [NSMutableArray arrayWithObjects:self.allArray,arr2,arr3,arr4,arr5,arr6, nil];
}

#pragma mark--reloadAndJosn
- (void)reloadAndJosn{
    
    [LORequestManger GET:kBrand success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *Dic in dic[@"result"][@"brandlist"]) {
            NSMutableArray *array = [[NSMutableArray alloc]init];
            NSArray *arr = Dic[@"list"];
            NSString *str = Dic[@"letter"];
            [self.dataArray addObject:str];
            for (NSDictionary *brand in arr) {
                
                BrandModel *model = [BrandModel shareJosnWithDic:brand];
                model.letter = str;
                [array addObject:model];
                
            }
            

            [self.allArray addObject:array];
        }
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ((int)self.index == 0) {
         NSLog(@"--%lu",(unsigned long)self.allArray.count);
        return self.allArray.count;
       
    }else

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"==%d",(int)self.index);
    if (self.index == 0) {
         NSLog(@"%lu",[self.array[0][section] count]);
        return [self.array[0][section] count];
       
    }else
        NSLog(@"%lu",[self.array[(int)self.index] count]);
    return [self.array[(int)self.index] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LTCell" forIndexPath:indexPath];
    if (self.index == 0) {
        cell.nameLable.text = [self.allArray[indexPath.section][indexPath.row] name];
        return cell;
    }else
    
    cell.nameLable.text = self.array[(int)self.index][indexPath.row];
    return cell;
    
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (self.index == 0) {
        return [self.allArray[section][0] letter];
    }
    return 0;
    
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    if (self.index == 0) {
    return self.dataArray;
    }
    return 0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    LTableViewCell *cell = (LTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    [cell setChecked];

}

- (void)buttonAction:(UIBarButtonItem *)button{
    [self.navigationController popViewControllerAnimated:YES];
    
}


@end
